R you ready to ggplot2?

Advanced Plots
November 10, 2014

Tony Fujs

Minard: Troops data set

 long  lat survivors direction group
 24.0 54.9    340000         A     1
 24.5 55.0    340000         A     1
 25.5 54.5    340000         A     1
 26.0 54.7    320000         A     1
 27.0 54.8    300000         A     1

Minard: Iteration 1

Plot troups location over time with a line


ggplot() +
geom_XXXX(
  aes(x = long,
      y = lat,
      group = group),
  data = troops)

plot of chunk unnamed-chunk-2

Minard: Iteration 1

Plot troups location over time with a line


ggplot() +
geom_path(
  aes(x = long,
      y = lat,
      group = group),
  data = troops)

plot of chunk unnamed-chunk-3

Minard: Iteration 2

Add color to show direction of the troups


# EDIT CODE
ggplot() +
geom_path(
  aes(x = long,
      y = lat,
      group = group),
  data = troops)

plot of chunk unnamed-chunk-4

Minard: Iteration 2

Add color to show direction of the troups


ggplot() +
geom_path(
  aes(x = long,
      y = lat,
      group = group,
      color = direction),
  data = troops)

plot of chunk unnamed-chunk-5

Minard: Iteration 3

Adjust line size to show number of survivors


# EDIT CODE
ggplot() +
geom_path(
  aes(x = long,
      y = lat,
      group = group,
      color = direction),
  data = troops)

plot of chunk unnamed-chunk-6

Minard: Iteration 3

Adjust line size to show number of survivors


ggplot() +
geom_path(
  aes(x = long,
      y = lat,
      group = group,
      color = direction,
      size = survivors),
  data = troops)

plot of chunk unnamed-chunk-7

Minard: Data set

 long  lat      city
 24.0 55.0     Kowno
 25.3 54.7     Wilna
 26.4 54.4  Smorgoni
 26.8 54.3 Moiodexno
 27.7 55.2 Gloubokoe

Minard: Iteration 4

Add cities on the plot (One point for each city)


# EDIT CODE 
# (You must add a complete layer)
ggplot() +
geom_path(
  aes(x = long,
      y = lat,
      group = group,
      color = direction,
      size = survivors),
  data = troops)

plot of chunk unnamed-chunk-9

Minard: Iteration 4

Add cities on the plot (One point for each city)


# EDIT CODE 
# (You must add a complete layer)
ggplot() +
geom_path(
  aes(x = long,
      y = lat,
      group = group,
      color = direction,
      size = survivors),
  data = troops) +
geom_point(
    aes(
      x = long,
      y = lat),
    data = cities)

plot of chunk unnamed-chunk-10

Minard: Iteration 5

Add city names on the plot beside each corresponding point


# EDIT CODE 
# (You must add a complete layer)
ggplot() +
geom_path(
  aes(x = long,
      y = lat,
      group = group,
      color = direction,
      size = survivors),
  data = troops) +
geom_point(
    aes(
      x = long,
      y = lat),
    data = cities)

plot of chunk unnamed-chunk-11

Minard: Iteration 5

Add city names on the plot beside each corresponding point


ggplot() +
geom_path(...) +
geom_point(...) +
geom_text(
       aes(x = long,
           y = lat,
           label = city),
       data = cities)

plot of chunk unnamed-chunk-12

Minard: Iteration 5

Something wrong with the labels?


# Deal with overlapping
ggplot() +
geom_path(...) +
geom_point(...) +
geom_text(
       aes(x = long,
           y = lat,
           label = city),
       data = cities)

plot of chunk unnamed-chunk-13

Minard: Iteration 5

Deal with overlapping


ggplot() +
geom_path() +
geom_point() +
geom_text(
        aes(x = long,
                y = lat - 0.03,
                label = city),
        data = cities)

plot of chunk unnamed-chunk-14

Minard: Iteration 5

City names: Reduce font size

(Setting size inside or outside aes()?)


ggplot() +
geom_path(XXXX) +
geom_point(XXXX) +
geom_text(
        aes(x = long,
                y = lat - 0.03,
                label = city),
        size = 4,
        data = cities)

plot of chunk unnamed-chunk-15

Minard: Iteration 6

Customize the line colors


# Which component of the gg does control the line colors?
ggplot() +
geom_path(XXXX) +
geom_point(XXXX) +
geom_text() +
?????

plot of chunk unnamed-chunk-16

Minard: Iteration 6

Customize the line colors


# Which component of the gg does control the line colors?
ggplot() +
geom_path(XXXX) +
geom_point(XXXX) +
geom_text() +
scale_color_manual(
  values = c('grey50','red'))

plot of chunk unnamed-chunk-17

Minard: Final

Walmart: US map data

      long      lat group order  region
 -87.46201 30.38968     1     1 alabama
 -87.48493 30.37249     1     2 alabama
 -87.52503 30.37249     1     3 alabama
 -87.53076 30.33239     1     4 alabama
 -87.57087 30.32665     1     5 alabama
 -87.58806 30.32665     1     6 alabama
 -87.59379 30.30947     1     7 alabama
 -87.59379 30.28655     1     8 alabama
 -87.67400 30.27509     1     9 alabama
 -87.81152 30.25790     1    10 alabama

Walmart: Stores data

 storenum      lat      long year new
        1 36.33203 -94.14905 1965   0
        1 36.33203 -94.14905 1970   0
        1 36.33203 -94.14905 1975   0
        1 36.33203 -94.14905 1980   0
        1 36.33203 -94.14905 1985   0
        1 36.33203 -94.14905 1990   0
        1 36.33203 -94.14905 1995   0
        1 36.33203 -94.14905 2000   0
        1 36.33203 -94.14905 2005   0
        2 36.26659 -93.13948 1965   0

Walmart: Quick look

Geographical information


ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_point()

plot of chunk unnamed-chunk-20

Walmart: Iteration 1

Initialize default ggplot & draw US map


ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_XXXX(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3")

plot of chunk unnamed-chunk-21

Walmart: Iteration 1

Initialize default ggplot & draw US map


ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3")

plot of chunk unnamed-chunk-22

Walmart: Iteration 2

Add points representing walmart stores


ggplot(aes(x = long, y = lat), data = walmart) +
  geom_polygon(aes(group = group), data = us, colour="black", fill="#F7F3F3") +
    ????

plot of chunk unnamed-chunk-23

Walmart: Iteration 2

Add points representing walmart stores


ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
  geom_point()

plot of chunk unnamed-chunk-24

Walmart: Iteration 3

Change point color if NEW store


ggplot(aes(x = long, y = lat), 
data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
    geom_point()

plot of chunk unnamed-chunk-25

Walmart: Iteration 3

Change point color if NEW store


ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
  geom_point(aes(color = new))

plot of chunk unnamed-chunk-26

Walmart: Iteration 4

Make sure geographic proportion are respected


ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
    geom_point(aes(color = new))

plot of chunk unnamed-chunk-27

Walmart: Iteration 4

Make sure geographic proportion are respected


ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
  geom_point(aes(color = new)) +
    coord_map()

plot of chunk unnamed-chunk-28

Walmart: Iteration 5

Create a small multiple (ONE chart per year)


ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
    geom_point(aes(color = new)) +
    coord_map()

plot of chunk unnamed-chunk-29

Walmart: Iteration 5

Create a small multiple (ONE chart per year)


ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
  geom_point(aes(color = new)) +
    coord_map() +
    facet_wrap(~year)

plot of chunk unnamed-chunk-30

Walmart: Iteration 6

Deal with overplotting (add transparency)


ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
    geom_point(aes(color = new)) +
    coord_map() +
    facet_wrap(~year)

plot of chunk unnamed-chunk-31

Walmart: Iteration 6

Deal with overplotting (add transparency)


ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
  geom_point(aes(color = new, 
              alpha = new)) +
    coord_map() +
    facet_wrap(~year)

plot of chunk unnamed-chunk-32

Walmart: Iteration 7

Deal with “new” treated as continuous


ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
    geom_point(aes(color = new, 
                alpha = new)) +
    coord_map() +
    facet_wrap(~year)

plot of chunk unnamed-chunk-33

Walmart: Iteration 7

Deal with “new” treated as continuous


ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
  geom_point(aes(color = factor(new),
                alpha = factor(new))) +
    coord_map() +
    facet_wrap(~year)

plot of chunk unnamed-chunk-34

Walmart: Iteration 8

Customize transparency


myAlpha <- c(.4, 0.7) 
ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
  geom_point(aes(color = factor(new), 
                alpha = factor(new))) +
  coord_map() +
  facet_wrap(~year)

plot of chunk unnamed-chunk-35

Walmart: Iteration 8

Customize transparency


myAlpha <- c(.4, 0.7) 
ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
  geom_point(aes(color = factor(new), 
                alpha = factor(new))) +
  coord_map() +
  scale_alpha_manual(values = myAlpha, 
                        guide = 'none') +
  facet_wrap(~year)

plot of chunk unnamed-chunk-36

Walmart: Iteration 9

Customize colors


myPalette <- c('#1F78B4', '#FF0066') 
ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
  geom_point(aes(color = factor(new),
                alpha = factor(new))) +
  coord_map() +
  scale_alpha_manual(values = myAlpha, 
                  guide = 'none') +
  facet_wrap(~year)

plot of chunk unnamed-chunk-37

Walmart: Iteration 9

Customize colors


myPalette <- c('#1F78B4', '#FF0066') 
ggplot(aes(x = long, y = lat), 
  data = walmart) +
  geom_polygon(aes(group = group), 
  data = us, 
  colour="black", fill="#F7F3F3") +
  geom_point(aes(color = factor(new), 
                alpha = factor(new))) +
  coord_map() +
  scale_alpha_manual(values = myAlpha, 
                    guide = 'none') +
  scale_colour_manual(
    values = myPalette,
    guide = 'none') +
  facet_wrap(~year)

plot of chunk unnamed-chunk-38

Walmart: Final